﻿using Spire.Doc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 文件创建
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
         /// 将两个word合并 也可以按最类似多个word合并
         /// </summary>
         /// <param name="path1"></param>
         /// <param name="path2"></param>
        public void WordMerge(string path1, string path2)
        {
            //加载文档1和文档2
            Document doc1 = new Document();
            doc1.LoadFromFile(path1, FileFormat.Docx);
            Document doc2 = new Document();
            doc2.LoadFromFile(path2, FileFormat.Docx);
            //获取文档2的第一个section
            Section sec = doc2.Sections[0];
            //克隆该section并把它添加到文档1
            doc1.Sections.Add(sec.Clone());
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".docx";
            doc1.SaveToFile(fileName, FileFormat.Docx);
        }

     

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFire = new OpenFileDialog();
            if (openFire.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFire.FileName;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFire = new OpenFileDialog();
            if (openFire.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = openFire.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty (textBox1.Text )&& !string.IsNullOrEmpty(textBox2.Text))
            {
                WordMerge(textBox1.Text, textBox2.Text);
            }
            else
            {
                MessageBox.Show("请选择要合并的文档");
            }
        }
    }
}
